package com.ruoyi.web.cp.service.rm;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.cp.domain.rm.RmResult;
import com.ruoyi.web.cp.service.http.RmHttpService;
import com.ruoyi.web.mapper.RmGameMapper;
import com.ruoyi.web.mapper.RmResultMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author xiesm
 * @Description: TODO
 * @date 2025-07-19 16:28
 */
@Service
public class RmResultService {
    @Autowired
    private RmHttpService rmHttpService;

    public void saveRmResult() {
        saveRmResult(null);
    }

    public void saveRmResult(String date) {
        Date dateTime = new Date();
        if (date != null) {
            dateTime = DateUtil.parse(date, "yyyy-MM-dd");
        }
        RmResultService service = SpringUtil.getBean("rmResultService", RmResultService.class);
        for (int i = 0; i < 2; i++) {
            Date targetDate = DateUtil.offsetDay(dateTime, -1 + i);
            String dateStr = DateUtil.format(targetDate, "yyyy-MM-dd");
            String res = rmHttpService.readRmResult(dateStr);
            Document document = Jsoup.parse(res);
            Elements gameList = document.getElementsByClass("game_list");
            for (Element element : gameList) {
                String attr = element.getElementsByClass("game_trade").attr("onclick");
                List<String> gameDatas = StringUtils.replace(attr, "getGameData");

                RmResult rmResult = new RmResult();
                rmResult.setDate(DateUtil.parse(gameDatas.get(2), "yyyy-MM-dd HH:mm"));
                rmResult.setTeamA(gameDatas.get(0));
                rmResult.setTeamB(gameDatas.get(1));
                rmResult.setHalfScore(gameDatas.get(3));
                rmResult.setAllScore(gameDatas.get(4));
                rmResult.setGoalTime(gameDatas.get(6));

                Date twoPointFiveHoursAgo = DateUtil.offsetMinute(dateTime, -(int) (2.5 * 60));
                if (rmResult.getDate().after(twoPointFiveHoursAgo)) {
                    // 如果在最近 2.5 小时内，跳过
                    continue;
                }

                service.saveOrUpdateByGameId(rmResult, targetDate);
            }
        }
    }

    @Transactional
    public void saveOrUpdateByGameId(RmResult entity, Date targetDate) {
        if (entity == null) {
            return;
        }
        RmGameMapper gameMapper = SpringUtil.getBean("rmGameMapper", RmGameMapper.class);
        // 1. 查询是否已存在记录
        String gameId = gameMapper.selectGameIdByTeamAAndTeamB(entity.getTeamA(), entity.getTeamB(), targetDate);
        if (gameId == null) {
            return;
        }
        entity.setGameId(gameId);
        RmResultMapper resultMapper = SpringUtil.getBean("rmResultMapper", RmResultMapper.class);
        RmResult existing = resultMapper.selectByGameId(gameId);
        // 2. 如果存在，设置其 ID 以便更新
        if (existing == null) {
            resultMapper.insertRmResult(entity);
        } else {
            resultMapper.updateRmResult(entity);
        }
    }
}
